<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label">订单编号</label>
        <el-input
          v-model="query.orderSn"
          clearable
          placeholder="订单编号"
          style="width: 185px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <label class="el-form-item-label">地区筛选</label>
        <el-input
          v-model="query.address"
          clearable
          placeholder="请输入地区名称--芝罘区"
          style="width: 185px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <!--       <label class="el-form-item-label">订单状态</label>
        <el-select v-model="query.orderStatus" placeholder="请选择订单状态" class="filter-item" style="width: 185px;"
                   @keyup.enter.native="crud.toQuery">
          <el-option
            v-for="item in dict.order_status"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>-->
        <label class="el-form-item-label">配货司机</label>
        <el-select
          v-model="query.distributionId"
          placeholder="请选择配货司机"
          class="filter-item"
          style="width: 185px;"
          @keyup.enter.native="crud.toQuery"
        >
          <el-option
            v-for="item in deliverList"
            :key="item.id"
            :label="item.nickName"
            :value="item.id"
          />
        </el-select>
        <el-date-picker
          v-model="query.createTime"
          :default-time="['00:00:00','23:59:59']"
          type="daterange"
          range-separator=":"
          size="small"
          class="date-item"
          value-format="yyyy-MM-dd HH:mm:ss"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
        />
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission">
        <el-button
          slot="right"
          class="filter-item"
          size="mini"
          type="primary"
          icon="el-icon-printer"
          @click="prints"
        >批量打印发货单</el-button>
      </crudOperation>
      <!--表单组件-->
      <el-dialog
        :close-on-click-modal="false"
        :before-close="crud.cancelCU"
        :visible.sync="crud.status.cu > 0"
        :title="crud.status.title"
        width="500px"
      >
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="订单编号" prop="orderSn">
            <el-input v-model="form.orderSn" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="会员ID" prop="userId">
            <el-input v-model="form.userId" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="地址" prop="receiverId">
            <el-input v-model="form.receiverId" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="订单总金额" prop="totalPrice">
            <el-input v-model="form.totalPrice" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="优惠金额" prop="discountsPrice">
            <el-input v-model="form.discountsPrice" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="支付金额" prop="payPrice">
            <el-input v-model="form.payPrice" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="抹零" prop="roundPrice">
            <el-input v-model="form.roundPrice" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="支付时间">
            <el-input v-model="form.payTime" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="优惠券ID">
            <el-input v-model="form.couponId" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="订单状态" prop="orderStatus">
            <el-input v-model="form.orderStatus" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="备注">
            <el-input v-model="form.remarks" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="创建时间" prop="createTime">
            <el-input v-model="form.createTime" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="运费（暂时不用）" prop="postage">
            <el-input v-model="form.postage" style="width: 370px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        :data="crud.data"
        size="small"
        style="width: 100%;"
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="orderSn" label="订单编号" />
        <el-table-column prop="userId" label="会员ID" />
        <el-table-column prop="receiverId" label="地址">
          <template slot-scope="scope">
            <span>{{ scope.row.receiver.address }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="totalPrice" label="订单总金额" />
        <el-table-column prop="discountsPrice" label="优惠金额" />
        <!--<el-table-column prop="couponId" label="优惠券ID"/>-->
        <el-table-column prop="createTime" label="订单时间">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="orderStatus" label="订单状态">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.orderStatus == 0"> {{ dict.label.order_status[scope.row.orderStatus] }}</el-tag>
            <el-tag v-else type="danger"> {{ dict.label.order_status[scope.row.orderStatus] }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          v-permission="['admin','ycOrder:edit','ycOrder:del']"
          fixed="right"
          label="操作"
          width="150px"
          align="center"
        >
          <template slot-scope="scope">
            <!--            <udOperation
                          :data="scope.row"
                          :permission="permission"
                        />-->
            <el-button v-if="scope.row.orderStatus == 0" type="primary" @click="deliverGoods(scope.row)">发货</el-button>
            <el-button v-else type="danger" disabled>已发货</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
      <!-- 返货 -->
      <el-dialog
        title="发货"
        :visible.sync="deliverGoodsDialog"
        width="30%"
      >
        <el-form :model="deliverForm">
          <el-form-item label="选择配送司机" label-width="120">
            <el-select v-model="deliverForm.distributionId" placeholder="请选择">
              <el-option
                v-for="item in deliverList"
                :key="item.id"
                :label="item.nickName"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="deliverGoodsDialog = false">取 消</el-button>
          <el-button type="primary" @click="goDeliverGoods">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import crudYcOrder from '@/api/order/assignOrder'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import crudYcUser from '@/api/user/user'

const defaultForm = {
  id: null,
  orderSn: null,
  userId: null,
  receiverId: null,
  totalPrice: null,
  discountsPrice: null,
  payPrice: null,
  roundPrice: null,
  payTime: null,
  couponId: null,
  orderStatus: null,
  remarks: null,
  createTime: null,
  postage: null
}
export default {
  name: 'YcOrder',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['order_status'],
  cruds() {
    return CRUD({ title: 'yc_order', url: 'api/order', sort: 'id,desc', crudMethod: { ...crudYcOrder }})
  },
  data() {
    return {
      permission: {
        add: ['admin', 'ycOrder:add'],
        edit: ['admin', 'ycOrder:edit'],
        del: ['admin', 'ycOrder:del']
      },
      rules: {
        orderSn: [
          { required: true, message: '订单编号不能为空', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'orderSn', display_name: '订单编号' },
        { key: 'userId', display_name: '会员ID' },
        { key: 'orderStatus', display_name: '订单状态' }
      ],
      //
      deliverList: [],
      deliverGoodsDialog: false,
      deliverForm: {
        orderSn: '',
        distributionId: ''
      },
      printsSn: []
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      console.log(this.crud.data)
      this.crud.optShow.add = false
      this.crud.optShow.edit = false
      this.crud.optShow.del = false
      //
      this.query.orderStatus = 1
      this.getDeliver()
      return true
    },
    [CRUD.HOOK.afterRefresh]() {
      console.log(this.crud.data)
      this.crud.data.forEach(item => {
        item.receiver = item.receiver.address
        console.log(item.receiver.address)
      })
      return true
    },
    // 发货
    deliverGoods(rows) {
      console.log(rows)
      const _t = this
      _t.deliverForm = {}
      _t.deliverGoodsDialog = true
      _t.deliverForm.orderSn = rows.orderSn
      // _t.getDeliver();
    },
    // 获取司机列表
    getDeliver() {
      const _t = this
      crudYcUser.getDriver().then(res => {
        _t.deliverList = res.content
        console.log(res)
      })
    },
    //
    goDeliverGoods() {
      const _t = this
      crudYcOrder.takeDelivery(_t.deliverForm.orderSn, _t.deliverForm.distributionId).then(res => {
        _t.deliverGoodsDialog = false
        _t.$message({
          message: '发货成功',
          type: 'success'
        })
        console.log(res)
        this.crud.refresh()
      })
      console.log(_t.deliverForm)
    },
    //
    prints() {
      const _t = this
      _t.printsSn = []
      _t.crud.selections.forEach((item, index) => {
        _t.printsSn[index] = item.orderSn
      })
      console.log(_t.printsSn)
      _t.goPrint(_t.printsSn)
    },
    goPrint(indentSn) {
      const routeUrl = this.$router.resolve({
        path: '/order/assignOrder/print',
        query: { indentSn: indentSn }
      })
      console.log(this.query)
      window.open(routeUrl.href, '_blank')
    }
  }
}
</script>

<style scoped>

</style>
